package com.supemarket.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.supemarket.dao.BaseDao;
import com.supemarket.dao.SelectSupemarketDao;
import com.supemarket.entity.Member;
import com.supemarket.entity.admin;
import com.supemarket.entity.commodity;
import com.supemarket.entity.purchase;
import com.supemarket.entity.sale;
import com.supemarket.entity.staff;
import com.supemarket.entity.supplier;

public class SelectSupemarketDaoimpl extends BaseDao implements SelectSupemarketDao{

	@Override
	public List<admin> admins() {
		String sql="select * from admin";
		ResultSet rs=this.executeQuery(sql);
		List<admin> list=new ArrayList<admin>();
		try {
			while(rs.next())
			{
				admin a=new admin(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getDate(5),rs.getDate(6),rs.getString(7));
				list.add(a);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<commodity> commodities() {
		String sql="select * from commodity";
		ResultSet rs=this.executeQuery(sql);
		List<commodity>list=new ArrayList<commodity>();
		try {
			while(rs.next())
			{
				commodity c=new commodity(rs.getString(1),rs.getString(2),rs.getInt(3),rs.getString(4),rs.getInt(5),rs.getInt(6),rs.getDate(7),rs.getDate(8),rs.getString(9));
				list.add(c);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Member> members() {
		String sql="select * from member";
		ResultSet rs=this.executeQuery(sql);
		List<Member> list=new ArrayList<Member>();
		try {
			while(rs.next())
			{
				Member m=new Member(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getDate(8),rs.getDate(9),rs.getDate(10),rs.getString(11));
				list.add(m);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<purchase> purchases() {
		String sql="select * from purchase";
		ResultSet rs=this.executeQuery(sql);
		List<purchase>list=new ArrayList<purchase>();
		try {
			while(rs.next())
			{
				purchase p=new purchase(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4),rs.getDate(5),rs.getString(6),rs.getDate(7),rs.getDate(8),rs.getString(9));
				list.add(p);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<sale> sales() {
		String sql="select * from sale";
		ResultSet rs=this.executeQuery(sql);
		List<sale>list=new ArrayList<sale>();
		try {
			while(rs.next())
			{
				sale s=new sale(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getDate(4),rs.getString(5),rs.getDate(6),rs.getDate(7),rs.getString(8));
				list.add(s);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<staff> staffs() {
		String sql="select * from staff";
		ResultSet rs=this.executeQuery(sql);
		List<staff> list=new ArrayList<staff>();
		try {
			while(rs.next())
			{
				staff s=new staff(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getDate(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getInt(8),rs.getDate(9),rs.getDate(10),rs.getString(11));
				list.add(s);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<supplier> suppliers() {
		String sql="select * from supplier";
		ResultSet rs=this.executeQuery(sql);
		List<supplier> list=new ArrayList<supplier>();
		try {
			while(rs.next())
			{
				supplier s=new supplier(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getDate(6),rs.getDate(7),rs.getString(8));
				list.add(s);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public admin denglu(String password, String username) {
		String sql ="SELECT * FROM admin WHERE username=? AND PASSWORD=?";
		ResultSet rs =this.executeQuery(sql,password,username);
		try {
			while(rs.next())
			{
				admin a=new admin(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getDate(5),rs.getDate(6),rs.getString(7));
				return a;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	

}
